﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using Common;



namespace web
{
    public partial class main : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BuildTree(); //临时使用；
                MainMenu.Attributes.Add("onclick", "MenuClick();");
              
            }
        }


        //protected void BuildTree()
        //{
        //    PmsHouse.BLL.FunctionList MainFunc = new PmsHouse.BLL.FunctionList();
        //    DataSet FuncList =  MainFunc.GetList(0,"StartFlag='1'", "FunctionNO");
        //    DataView FuncListMainV = new DataView(FuncList.Tables[0]);
        //    FuncListMainV.RowFilter = "[FunctionRel] is null";
        //    foreach (DataRowView MainMenu in FuncListMainV)
        //    { //一级菜单

        //        NavMenuItem theItem = new NavMenuItem();
        //        theItem.Text = MainMenu["FunctionName"].ToString();
        //        theItem.CssClass = "create";
        //        NavMenu1.ListItems.Add(theItem);
        //        DataView SubFunc = new DataView(FuncList.Tables[0]);
        //        SubFunc.RowFilter = "[FunctionRel] =" + MainMenu["FunctionID"];
        //        foreach (DataRowView SubMenu in SubFunc)
        //        { //二级菜单
        //            NavMenuSubItem theItem2 = new NavMenuSubItem();
        //            theItem2.Text = SubMenu["ImagePath"].ToString() + SubMenu["FunctionName"].ToString();
        //            theItem2.Title = SubMenu["FunctionName"].ToString();
        //            theItem2.NavigationURL = SubMenu["FunctionApp"].ToString();
        //            theItem2.Target = "mainframe";
        //            theItem.Items.Add(theItem2);



        protected void BuildTree()
        {

            //  MainMenu.Nodes.Clear();

            try
            {
                string sql = "select * FROM FunctionList where StartFlag='1'order by FunctionNO";
                DataSet ds = DbHelperSQL.Query(sql);
                this.ViewState["ds"] = ds;
                AddTree("", (TreeNode)null);
                MainMenu.CollapseAll();
            }
            catch (Exception e)
            {

                LogService.Write("BuildMainMenu 运行失败");
                LogService.Write(e.Message);
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('数据库错误！请联系系统管理员')", true);
                return;

            }

            return;

        }

        public void AddTree(string ParentID, TreeNode pNode)
        {

            DataSet ds = (DataSet)this.ViewState["ds"];
            DataView dvTree = new DataView(ds.Tables[0]);


            //过滤父节点,得到这个父节点当前的所有子节点

            if (ParentID == "") dvTree.RowFilter = "[FunctionRel] is null ";
            else dvTree.RowFilter = "[FunctionRel] = " + ParentID + "";


            foreach (DataRowView Row in dvTree)
            {


                TreeNode Node = new TreeNode();
                if (pNode == null)
                {    //添加根节点
                    Node.Text = Row["FunctionName"].ToString();
                    Node.Value = Row["FunctionID"].ToString();
                    Node.NavigateUrl = "";
                    Node.SelectAction = TreeNodeSelectAction.Expand;
                    MainMenu.Nodes.Add(Node);
                    Node.Expanded = true;

                    AddTree(Row["FunctionID"].ToString(), Node);    //再次递归
                }
                else
                {   //̀添加当前节点的子节点


                    Node.Text = Row["FunctionName"].ToString();
                    Node.Value = Row["FunctionID"].ToString();
                    if (!string.IsNullOrEmpty(Row["FunctionApp"].ToString()))
                    {
                        Node.NavigateUrl = "javascript:void('" + Row["FunctionApp"].ToString() + "');";
                    }
                    
                    pNode.SelectAction = TreeNodeSelectAction.Expand;
                    pNode.ChildNodes.Add(Node);
                    Node.Expanded = true;

                    AddTree(Row["FunctionID"].ToString(), Node);     //再次递归

                }
            }

        }

    }
}
   